Golang : Reclaim memory occupied by make() example
Problem :
You have allocated some memory for buffer via the make()
function and you want to reclaim back the occupied memory after done using the buffer. How to do that?
Solution :
For example :
buffer := make([]byte, 1024)
or
buffer := make([][]int, row)
to reclaim back the memory, simply do :
buffer = nil
Golang's garbage collector will automatically free up the memory or you can trigger garbage collection manually as well with runtime/debug.FreeOSMemory()
References :
https://www.socketloop.com/tutorials/golang-how-to-get-garbage-collection-data
https://www.socketloop.com/tutorials/golang-when-to-use-make-or-new
See also : Golang : When to use make or new?
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+9.4k Random number generation with crypto/rand in Go
+8.5k Golang : Executing and evaluating nested loop in html template
+10k Golang : Wait and sync.WaitGroup example
+16.1k Golang :Trim white spaces from a string
+34.2k Golang : How to stream file to client(browser) or write to http.ResponseWriter?
+18.8k Golang : Clearing slice
+15.2k Golang : How to convert(cast) IP address to string?
+7.1k Golang : Accessing dataframe-go element by row, column and name example
+9.7k Golang : Setting variable value with ldflags
+15.6k Golang : Read large file with bufio.Scanner cause token too long error